package org.road0329.dlt698.axdr;

import org.road0329.dlt698.ReverseByteArrayInputStream;
import org.road0329.dlt698.ReverseByteArrayOutputStream;
import org.road0329.dlt698.asn1.axdr.AxdrType;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.ArrayList;

class ProxySetResponse extends AxdrType {
    ByteBuffer dataCode = null;
    TSA tsa = null;
    SubSeqOfResult result = null;

    public ProxySetResponse() {
    }

    public void set_dataCode(ByteBuffer dataCode) {
        this.dataCode = dataCode;
    }

    public void set_all(TSA tsa, SubSeqOfResult result) {
        this.tsa = tsa;
        this.result = result;
    }

    @Override
    public int decode(ReverseByteArrayInputStream input) throws Exception {
        int codeLength = 0;
        this.tsa = new TSA();
        codeLength += this.tsa.decode(input);
        this.result = new SubSeqOfResult();
        codeLength += this.result.decode(input);
        return codeLength;
    }

    @Override
    public int encode(ReverseByteArrayOutputStream output) throws Exception {
        int codeLength = 0;
        if (this.dataCode != null) {
            codeLength = this.dataCode.remaining();
            while (this.dataCode.hasRemaining()) {
                output.write(this.dataCode.get());
            }
        } else {
            if (this.result != null && this.tsa != null) {
                codeLength = 0;
                codeLength += this.result.encode(output);
                codeLength += this.tsa.encode(output);
            } else {
                throw new RuntimeException("tsa || result is null");
            }
        }
        return codeLength;
    }

    public void encodeAndSave(int encodingSizeGuess) throws Exception {
        ReverseByteArrayOutputStream revOStream = new ReverseByteArrayOutputStream();
        revOStream.setBufSize(encodingSizeGuess);
        this.encode(revOStream);
        this.dataCode = ByteBuffer.wrap(revOStream.toByteArray());
    }

    @Override
    public String toString() {
        if (this.tsa != null && this.result != null) {
            return "sequence: {" + "tsa: " + this.tsa + ", result: " + this.result + "}";
        } else {
            return "tsa || result is null";
        }
    }

    class SubSeqOfResult extends ArrayList<ProxySet> {
        public ProxySet createListElement() {
            return new ProxySet();
        }

        public SubSeqOfResult() {
            super();
        }

        public void set_length(int length) {
            // In Java, the size of the list is managed automatically
        }

        public int decode(ReverseByteArrayInputStream input) throws Exception {
            // Implement the decode logic here
            // This is a placeholder implementation
            int codeLength = 0;
            // ... decode logic
            return codeLength;
        }

        public int encode(ReverseByteArrayOutputStream output) throws Exception {
            // Implement the encode logic here
            // This is a placeholder implementation
            int codeLength = 0;
            // ... encode logic
            return codeLength;
        }
    }
}



